import Window
from ui import HTML,Button,Label,HorizontalPanel,VerticalPanel,ScrollPanel,HasAlignment,Composite,Hyperlink,ListBox
from Sink import Sink,SinkInfo
from service import DataService
from __pyjamas__ import console

class TitlePanel(Composite):
    def __init__(self):
        self.content = HTML("")

        self.panel = HorizontalPanel()
        self.panel.setWidth("100%")
        
        self.panel.add(self.content)
        
        
        self.initWidget(self.panel)
    def setTitle(self,title):
        self.content.setHTML(title)
        
class FooterPanel(Composite):
    def __init__(self):
        self.content = HTML("""<div align="center">Powered by <a href="http://appengine.google.com">Google App Engine</a>,Designed by <a href="mailto:godtime123@gmail.com">GodTime</a><br/>
                    Thanks for:<a href="http://pyjamas.sf.net">Pyjamas</a>,<a href="http://appengine.google.com">GAE-JSONRPC</a>
                    <br/>
                     V  0.1.20090101
</div>""",False)

        self.panel = HorizontalPanel()
        self.panel.setWidth("100%")
        
        self.panel.add(self.content)
        
        
        self.initWidget(self.panel)        
class PagePanel(Composite):
    def __init__(self,mainPanel):
        self.mp = mainPanel

        des1 = Label("Page:")
        self.des2 = Label("")
        self.sel = ListBox()
        self.sel.addChangeListener(getattr(self,"onSelChange"))
        container = HorizontalPanel()
        container.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
        container.add(des1)
        container.add(self.sel)
        container.add(self.des2)      
        
        container.setCellWidth(des1, "40px")
        container.setCellWidth(self.sel,"60px")
          
        self.initWidget(container)
        
        self.setStyleName('gtBlogPager')
    def setArtiCount(self,n):
        
        pc= (int)(n/10)+1
        if n%10 == 0:
                pc = pc - 1
        for ii in range(1,pc+1):
            self.sel.addItem(str(ii) )
        self.des2.setText("Articals:"+str(n) )
    def onSelChange(self):
        n = self.sel.getSelectedIndex()
        self.mp.ShowPage(n)

    def setSelectAsync(self,num):
        self.sel.setSelectedIndex(num)

class ArticalPanel(Composite):
    def __init__(self):
        self.doBlog = DataService()
        self.container = VerticalPanel()          
        self.initWidget(self.container)
        self.setStyleName("gtArtical")
    
    def ShowArticals(self,n):
        self.doBlog.blogShow(n,self)
    def onRemoteResponse(self, response, request_info):  
        self.container.clear()
        loc = Window.getLocation()
        for artical in response:
            ap = VerticalPanel()
            ap.setStyleName("gtArtical")
            s = """<a href=" """ + loc.getPathname()+ """gtBlog?aid=""" + artical[0] +""" " target="_blank">""" +artical[1]+ """</a> """
            ln1 = HTML(s,False)
            ln1.setStyleName("gtArticalTitle")
            
            sf = "time:"+artical[4]+"    visits:"+artical[5]+"    comments:"+artical[6]
            footer = HTML(sf)
            footer.setStyleName("gtArticalFooter")
            
            content = HTML(artical[3])
            content.setStyleName("gtArticalContent")
            ap.add(ln1)
            ap.add(content)
            ap.add(footer)    
            self.container.add(ap)
    def onRemoteError(self, code, message, request_info):
        pass
            
class TIBlog(Sink):
    #sSide ="""<iframe src="//www.google.com/calendar/embed?showTitle=0&amp;showNav=0&amp;showPrint=0&amp;showTabs=0&amp;showCalendars=0&amp;showTz=0&amp;mode=AGENDA&amp;height=300&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=ki38qmo6ujlkpjra6cf8jk80hs%40group.calendar.google.com&amp;color=%23B1365F&amp;ctz=Asia%2FShanghai" style=" border-width:0 " width="290" height="300" frameborder="0" scrolling="no"></iframe>"""
    sSide = """<iframe props="align:left" style="display: block; margin-top: 5px; margin-bottom: 0pt; text-align: left;" src="http://209.gmodules.com/ig/ifr?mid=209&amp;synd=trogedit&amp;url=http%3A%2F%2Fwww.google.com%2Fig%2Fmodules%2Fchina%2Fccal.xml&amp;h=270&amp;w=280" allowtransparency="true" scrolling="no" frameborder="0" class="igm" igsrc="http://www.google.com/chart?chc=sites&amp;cht=d&amp;chdp=sites&amp;chl=%5B%5BGoogle+%E5%B0%8F%E5%B7%A5%E5%85%B7%27%3D16%27f%5Cbf%5Chv%27a%5C%3D138%270%27%3D137%270%27dim%27%5Cbox1%27b%5CDBD9BB%27fC%5CDBD9BB%27eC%5C15%27sk%27%5C%5B%E2%80%9C%E4%B8%AD%E5%9B%BD%E5%86%9C%E5%8E%86%E2%80%9D%27%5Dh%27a%5CV%5C%3D12%27f%5Cbf%5C%5DV%5Cta%5C%3D139%27%3D0%27%3D138%27%3D267%27dim%27%5C%3D139%27%3D0%27%3D138%27%3D267%27vdim%27%5Cbox1%27b%5Cva%5CFFFEF0%27fC%5CDBD9BB%27eC%5Csites_gadget%27i%5Chv-0-0%27a%5C%5Do%5CLauto%27f%5C&amp;sig=Bp_7OTTxpibkbiT3zNm-Q5lzisI" width="280" height="270"></iframe>"""
    def __init__(self):  
        self.showHandler = DataService()

        self.loginPanel = TitlePanel()
        self.loginPanel.setStyleName("gtBlogTitle")
        
        #
        self.footerPanel = FooterPanel()
        self.footerPanel.setStyleName("gtBlogFooter")
        ###        

        self.pager = PagePanel(self)
        self.pager1 = PagePanel(self)
        self.artiPanel = ArticalPanel()
        
        self.content = VerticalPanel()
        self.content.setStyleName("gtBlogArticals")
        self.content.add(self.pager1)
        self.content.add(self.artiPanel)
        self.content.add(self.pager)
        ###

        
        self.sidebar = VerticalPanel()
        self.sidebar.setStyleName("gtBlogSidebar")
        #self.sidebar.add(HTML(self.sSide),True)
        #self.sidebar.setSize("100%", "100px")

        ###
        ###
        self.body = HorizontalPanel()
        self.body.setStyleName("gtBlogBody")
        #self.body.setWidth("100%")
        #self.body.setHeight("300px")

        self.body.add(self.content)
        self.body.add(self.sidebar)
        #self.body.setCellWidth(self.content, "61%")
        self.body.setCellWidth(self.sidebar, "300px")
        
        self.vp = VerticalPanel()
        self.vp.setStyleName("gtBlog")   
        self.vp.add(self.loginPanel)    
        self.vp.add(self.body)
        self.vp.add(self.footerPanel)
        
        self.container = ScrollPanel()
        self.container.add(self.vp)
        
        self.setWidget(self.container)
        
        self.showHandler.getPages(self)
        self.artiPanel.ShowArticals(0,self)
    def onShow(self):
        self.container.setSize(self.cx, self.cy)
        self.vp.setSize(self.cx-18, "100%")
        return

    def onRemoteResponse(self, response, request_info):
        self.pager.setArtiCount(response['count'])
        self.pager1.setArtiCount(response['count'])
        sidebars = response['sidebar']
        self.loginPanel.setTitle(sidebars[0])
        sb = sidebars[1:]
        for sb in sb:
            self.sidebar.add( HTML(sb,True) )

    def onRemoteError(self, code, message, request_info):
        pass
    
    def ShowPage(self,num):
        self.artiPanel.ShowArticals(num,self)
        self.pager.setSelectAsync(num)
        self.pager1.setSelectAsync(num)
        
def init():
    return SinkInfo("Blog", "<b>Blog</b>", TIBlog)